package com.xxwy.dependency.injection.field;

import com.xxwy.dependency.User;
import com.xxwy.dependency.UserHold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * 注解注入
 * @author wy
 * @date 2020/4/26 15:54
 */
public class AnnotationDependencyFieldInjectionDemo {

  /*@Autowired
  private UserHold userHold;
  */
  @Resource
  private UserHold userHold;

  public static void main(String[] args) {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(AnnotationDependencyFieldInjectionDemo.class);
    XmlBeanDefinitionReader definitionReader = new XmlBeanDefinitionReader(context);
    int definitions = definitionReader.loadBeanDefinitions("classpath:/META-INF/dependency-lookup-context.xml");
    context.refresh();

    AnnotationDependencyFieldInjectionDemo bean = context.getBean(AnnotationDependencyFieldInjectionDemo.class);
    System.out.println(bean.userHold);

    context.close();
  }

  @Bean
  public UserHold userHold(User user) {
    return new UserHold(user);
  }
}
